package com.huawei.esdk.uc.sametime.uccallbackimpl;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.jna.Constants.ContactAvailability;
import com.huawei.esdk.uc.jna.Constants.VideoCallStatus;
import com.huawei.esdk.uc.jna.callback.VideoCallEventCB;
import com.huawei.esdk.uc.jna.struct.STVideoCallParam;
import com.huawei.esdk.uc.jna.util.StringUtil;
import com.huawei.esdk.uc.sametime.widgets.CallComeInPopup;
import com.huawei.esdk.uc.sametime.widgets.EsdkUCConvWindow;
import com.ibm.collaboration.realtime.people.PeopleUtil;

public class UCVideoCallBackImpl implements VideoCallEventCB
{

    private Logger logger = Logger.getLogger(UCVideoCallBackImpl.class);

    private static UCVideoCallBackImpl instance = new UCVideoCallBackImpl();

    public synchronized static UCVideoCallBackImpl getInstance()
    {
        return instance;
    }

    private UCVideoCallBackImpl()
    {

    }

    @Override
    public void callBack(final STVideoCallParam stvideocallparam)
    {
        logger.debug("VideoCallEventCB | ucAcc = "
                + StringUtil.bytesToString(stvideocallparam.ucAcc)
                + ", callStatus = " + stvideocallparam.callStatus);

        // 发布自己的状态
        UCService.getInstance().UC_SDK_PubSelfStatus(ContactAvailability.Busy,
                "");
        PeopleUtil.getLocalPerson().refreshStatus();

        if (VideoCallStatus.CALL_VIDEO_REQ == stvideocallparam.callStatus)
        {
            logger.debug("VideoCallStatus.CALL_VIDEO_REQ message received");
            ShowComeInPopupJob showComeInPopupJob = new ShowComeInPopupJob(
                    stvideocallparam);
            Display.getDefault().syncExec(showComeInPopupJob);
        }
        else if (VideoCallStatus.CALL_VIDEO_CLOSE == stvideocallparam.callStatus)
        {
            logger.debug("VideoCallStatus.CALL_VIDEO_CLOSE message received");
            CloseVideoScreenJob closeJob = new CloseVideoScreenJob();
            Display.getDefault().syncExec(closeJob);
        }
        else
        {
            logger
                    .debug("VideoCallStatus.CALL_VIDEO_CONNECTED message received");
            AddVideoSrcreenJob addJob = new AddVideoSrcreenJob();
            Display.getDefault().syncExec(addJob);
        }

    }

    private static class ShowComeInPopupJob implements Runnable
    {
        private STVideoCallParam videoParam;

        public ShowComeInPopupJob(STVideoCallParam videoParam)
        {
            this.videoParam = videoParam;
        }

        @Override
        public void run()
        {
            CallComeInPopup.getInstance().showComeInPopup(this.videoParam);
        }
    }

    private static class CloseVideoScreenJob implements Runnable
    {
        @Override
        public void run()
        {
            EsdkUCConvWindow.getInstance().closeVideoScreen();
        }
    }

    private static class AddVideoSrcreenJob implements Runnable
    {
        @Override
        public void run()
        {
            EsdkUCConvWindow.getInstance().addVideoScreenArea();
        }
    }
}
